import { conn } from "@beshell/conn-ts/Connection"
import { Stud } from "../Stud"
import { Usage } from "../StudUsage"
import { Cable } from "./Cable"

export class UART extends Cable {
    constructor(title: string, public name: string, baudrate: number, mosi?: Stud, miso?: Stud) {
        super("uart", title, name, baudrate)
        this.mapDefaultHostStuds[Usage.uartmosi] = mosi   // master tx, slaver rx
        this.mapDefaultHostStuds[Usage.uartmiso] = miso   // master rx, slaver tx
    }

    public generateSetupCode(indent=0, freeFirst=false) {
        if(conn.isConnected) {
            return ''
        }
        let mosi = this.mapHostStuds[Usage.uartmosi]
        let miso = this.mapHostStuds[Usage.uartmiso]
        if( !mosi || !miso)  {
            return ''
        }
        return `${"\t".repeat(indent)}${this.name}.setup(${this.baudrate}, {tx: ${mosi.info.pin}, rx: ${miso.info.pin} })\n`
    }
}
